/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socof_webcrawler;

import DataBase.Coleccao;
import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;

public class ControllerParser extends Thread {

    private static ControllerParser pc = new ControllerParser();
    private static final int WAIT_TIME = 1000;
//    private Coleccao cl = new Coleccao();
    Coleccao cl = Coleccao.getInstance();

    private ControllerParser() {
    }

    public static ControllerParser getInstance() {
        return pc;
    }

    @Override
    public void run() {
        LaunchParsers pl = LaunchParsers.getInstance();
        OperatingSystemMXBean mx = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        double cpuUsage, maxCpuUsage;

        try {
            while (!isInterrupted()) {
                if (cl.listaVazia() == true) {
                    pl.removeAllParsers();
                } else {
                    maxCpuUsage = pl.getMaxCpuUsage();
                    cpuUsage = mx.getSystemCpuLoad();
                    if (cpuUsage < maxCpuUsage) {
                        pl.increaseParsers();
                    } else {
                        pl.decreaseParsers();
                    }
                }
                Thread.sleep(WAIT_TIME);
            }
        } catch (InterruptedException ex) {
        }
    }
}
